/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.proyectos.converters;

import com.proyectos.ejb.CarEjbLocal;
import com.proyectos.model.Carreras;
import java.math.BigDecimal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Rafael
 */

public class CarConverter implements Converter {
    CarEjbLocal carEjb = lookupCarEjbLocal();
  

   

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        try {
            BigDecimal id= new BigDecimal(value);
            Carreras car= this.carEjb.get(id);
            return car;
        } catch (Exception ex) {
            Logger.getLogger(CarConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Carreras car = (Carreras) value;
        return car.getNcodigo().toString();
    }

    private CarEjbLocal lookupCarEjbLocal() {
        try {
            Context c = new InitialContext();
            return (CarEjbLocal) c.lookup("java:global/Proyectos2/Proyectos2-ejb/CarEjb!com.proyectos.ejb.CarEjbLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

  

  

  
    
}
